package app.controllers.admin;

import app.Const;
import app.models.wx.WxTicket;
import app.models.member.Member;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

/**
 * <p>
 * .
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-02-12 13:41
 * @since JDK 1.6
 */
public class TicketController extends BasicController {
    public void index() {
        render("index.ftl");
    }

    public void list() {
        Member member = getModel(Member.class, "member");
        WxTicket ticket = getModel(WxTicket.class, "ticket");
        String uploadTime = getPara("uploadTime");
        final DatatablesCriterias criterias = getCriterias();
        Page<WxTicket> ticketPage = WxTicket.dao.customerTickets(criterias, member, ticket, uploadTime);

        renderDataTables(ticketPage, criterias);
    }

    public void exchange() {
        long ticketId = getParaToLong("ticketId", 0l);
        long memberId = getParaToLong("memberId", 0l);
        String integral = getPara("integral");
        String ticketNo = getPara("ticketNo");

        WxTicket ticket = new WxTicket();
        ticket.set("id", ticketId);
        ticket.set("added", true);
        ticket.set("add_time", new Date());
        ticket.set("ticket_no", ticketNo);
        ticket.set("integral", integral);
        ticket.update();

        if (!Strings.isNullOrEmpty(integral)) { //积分操作
            Double d = Double.parseDouble(integral);
            Member.dao.optIntegral(memberId, true, d, Const.TICKET_EXC_REASON);
        }
        renderJson(AjaxMessage.ok("兑换成功"));

    }

    public void pricute() {
        long ticketId = getParaToLong(0, 0l);
        WxTicket wxTicket = WxTicket.dao.findById(ticketId);
        setAttr("wxTicket", wxTicket);
        render("pricute.ftl");
    }

}
